home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ARASAN_S.ZIP / ARRAY.H < prev    next >
C/C++ Source or Header  |  1993-12-16  |  4KB  |  163 lines

  1. // Copyright 1993 by Jon Dart.  All Rights Reserved.
  2.  
  3. #ifndef _ARRAY_H
  4. #define _ARRAY_H
  5.  
  6. #include <stddef.h>
  7. #include <assert.h>
  8. #include "types.h"
  9.  
  10. typedef unsigned Index;
  11.  
  12. template <class Contents>
  13. class Array 
  14. {
  15. public:
  16.   // defines a simple, type-safe, expandable array class.
  17.  
  18.   Array( const Index initial_size = 100, const Boolean initialize = True );
  19.  
  20.   virtual ~Array();
  21.  
  22.   Array( const Array<Contents> & );
  23.       // copy constructor
  24.  
  25.   Array<Contents> & operator = (const Array<Contents> & );
  26.       // assignment operator
  27.  
  28.   Contents &operator [] (const Index) const;
  29.       // Note: we return a reference so that array[k] = stuff works.
  30.           
  31.   const Contents *get_data() const
  32.   {
  33.       return data;
  34.   }
  35.  
  36.   int size() const
  37.   {
  38.       // return the last valid index + 1
  39.       return data_size;
  40.   }
  41.  
  42.   int allocated_size() const
  43.   {
  44.       // return the number of array elements allocated (may != data_size).
  45.       return alloc_size;
  46.   }
  47.  
  48.   void resize( const Index new_size );
  49.       // Expand the valid, indexable number of items in the array.
  50.       // Items beyond the current limit are not initialized.  An
  51.       // array can be shrunk with this function, but only the last
  52.       // valid index is changed.  Storage is not reclaimed.          
  53.  
  54.   Array<Contents> &operator += (const Contents &Data );
  55.   Array<Contents> &operator += (const Array<Contents> &Data );
  56.       // Note: items added to the array are copies of the original data.
  57.  
  58. private:
  59.   void expand_storage( const Index new_size );
  60.       // Expands the internal storage in the array to allow holding
  61.       // at least "new_size" items.  This does not increase the maximum
  62.       // valid index.
  63.           
  64.   Contents *data;
  65.   Index data_size, alloc_size;
  66. };
  67.  
  68. template <class Contents>
  69. Array<Contents>::Array( const Index initial_size, const Boolean initialize )
  70. {
  71.    data_size = alloc_size = 0;
  72.    data = NULL;    
  73.    // If "initialize" = True, we want an array filled with data; otherwise,
  74.    // we want to allocate storage but consider the array empty.
  75.    if (initialize)         
  76.       resize( initial_size );
  77.    else
  78.       expand_storage(initial_size);
  79. }
  80.  
  81. template <class Contents>
  82. Array<Contents>::~Array()
  83. {
  84.     delete [] data;
  85. }
  86.  
  87. template <class Contents>
  88. Array<Contents>::Array( const Array<Contents> &arr)
  89. {
  90.      data_size = arr.data_size;
  91.      alloc_size = arr.alloc_size;
  92.      data = new Contents [arr.alloc_size];
  93.      assert(data);
  94.      for (Index i = 0; i < data_size; i++)
  95.          data[i] = arr.data[i];
  96. }
  97.  
  98. template <class Contents>
  99. Array <Contents>& Array<Contents>::operator = (const Array<Contents> &arr)
  100. {
  101.     if (this != &arr)
  102.     {
  103.         data_size = arr.data_size;
  104.         alloc_size = arr.alloc_size;
  105.         data = new Contents [arr.alloc_size];
  106.         for (Index i = 0; i < data_size; i++)
  107.             data[i] = arr.data[i];
  108.     }
  109.     return *this;
  110. }
  111.  
  112. template <class Contents>
  113. Contents& Array<Contents>::operator [](const Index index) const
  114. {
  115. #ifdef RANGE_CHECK
  116.     assert (index < data_size);
  117. #endif
  118.     return data[index];
  119. }
  120.  
  121. template <class Contents>
  122. Array<Contents> &Array<Contents>::operator += (const Contents &Data )
  123. {
  124.     Index old_data_size = data_size;
  125.     resize(data_size + 1);
  126.     data[old_data_size] = Data;
  127.     return *this;
  128. }
  129.  
  130. template <class Contents>
  131. Array<Contents> &Array<Contents>::operator += (const Array <Contents>&Data )
  132. {
  133.     Index old_size = data_size;
  134.     resize(data_size + Data.size());
  135.     for (Index i = old_size; i < data_size; i++)
  136.         data[i] = Data.data[i-old_size];
  137.     return *this;
  138. }
  139.  
  140. template <class Contents >
  141. void Array<Contents>::expand_storage( const Index new_size )
  142. {
  143.     Index size1 = alloc_size + alloc_size/10 + 10;
  144.     Index real_new_size = (new_size > size1) ? new_size : size1;
  145.     Contents * new_data = new Contents[real_new_size];
  146.     assert(new_data);
  147.     for (Index i = 0; i<data_size; i++)
  148.         new_data[i] = data[i];
  149.     delete [] data;
  150.     data = new_data; 
  151.     alloc_size = real_new_size;
  152. }
  153.  
  154. template <class Contents>
  155. void Array<Contents>::resize( const Index new_size )
  156. {
  157.    if (new_size > alloc_size)
  158.       expand_storage( new_size );
  159.    data_size = new_size;
  160. }
  161.  
  162. #endif
  163.